package com.dminvoicemanager.itemmaster;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import com.dminvoicemanager.DM_InvoiceMenu;
import com.dminvoicemanager.R;
import com.dminvoicemanager.itemmaster.DM_ItemsMaster_Main_Tabs;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class DM_Item_Master extends Activity {
	private String DBNAME = "dminvoicemanager.db";
	private String TOWN_DETAILS = "fTown";
	private String ITEM_DEBTOR = "fDebtor";
	private String ITEMMASTER_SUM = "FItemsMasterSum";
	TextView txtHeading;
	SQLiteDatabase mydb;
	String itemMasterCode;

	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(), DM_InvoiceMenu.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		myIntent.putExtra("TAGID", "SALES");
		startActivityForResult(myIntent, 0);
		finish();
	}
	
	public void DisplayItemsSumDetails(View view) {
		Button retailerDet = (Button) view;
		itemMasterCode = retailerDet.getText().toString();
		updateItemsMasterSum();

		Intent myIntent = new Intent(view.getContext(),
				DM_ItemsMaster_Main_Tabs.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();

	}
	
	
	private void updateItemsMasterSum() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		try {
			ContentValues addItemsMasSum = new ContentValues();
			addItemsMasSum.put("ItemCode", itemMasterCode);
			addItemsMasSum.put("ItmSumaddDate", getCurrentTimeStringCustom()
					.toString());
			mydb.insert(ITEMMASTER_SUM, null, addItemsMasSum);
		} catch (Exception e) {
		}
		mydb.close();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_items_master);
		txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Items Master");

		
		
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		mydb.execSQL("DROP TABLE IF EXISTS FItemsMasterSum");
		try {
			mydb.execSQL("create table if not exists "
					+ ITEMMASTER_SUM
					+ "(itmsum_id integer primary key autoincrement,ItemCode text not null,ItmSumaddDate text not null);");
		} catch (Exception e) {
		}
		mydb.close();
		DisplayItemsList();
	}

	private void DisplayItemsList() {
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dmitemslistview);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2", "coldes_3" };
		int[] totrdes = new int[] { R.id.itemrawname, R.id.itemrawbrand, R.id.itemsrawsummary };
		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();

		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curListItems = null;
		try {
			String query = "SELECT FI.ItemCode,FI.ItemName,FI.BrandCode,FB.BrandCode,FB.BrandName from fItems FI , fbrand FB Where FI.BrandCode = FB.BrandCode";
			curListItems = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}

		if (curListItems != null) {
			if (curListItems.getCount() > 0) {
				while (curListItems.moveToNext()) {
					String strItemName  = curListItems.getString(1);
					String strItemCode  = curListItems.getString(0);
					String strBrandCode = curListItems.getString(2);
					String strBrandName = curListItems.getString(4);
					
					/*
					 * String retailerDetSum = strDebCode + "/" + strDebName +
					 * "/" + strTownCode + "/" +strTownName;
					 */

					HashMap<String, String> maptrdes = new HashMap<String, String>();
					maptrdes.put("coldes_1", strItemName);
					maptrdes.put("coldes_2", strBrandName);
					maptrdes.put("coldes_3", strItemCode);
					fillMapstrdes.add(maptrdes);

				}
				// fill in the grid_item layout
				SimpleAdapter adaptertrdes = new SimpleAdapter(this,
						fillMapstrdes, R.layout.dm_items_master_raw,
						fromtrdes, totrdes);
				lvtypedes.setAdapter(adaptertrdes);
			}
		}
		curListItems.close();
		mydb.close();
	}

	private String getCurrentTimeStringCustom() {
		// 2013-10-04 00:00:00.000
		Date dateNow = new Date();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat ddMMMyyFormat = new SimpleDateFormat("dd/MM/yyyy");
		String date_to_string = ddMMMyyFormat.format(dateNow);
		return date_to_string;
	}

	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}

}
